/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lt.ntec.danblog.server.core.feed.atom;

import com.google.inject.Injector;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lt.ntec.danblog.server.blog.Service;
import lt.ntec.danblog.server.blog.model.Post;
import lt.ntec.danblog.server.core.Config;
import lt.ntec.danblog.server.core.GuiceFactory;
import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;

/**
 *
 * @author Tadas
 */
public class FeedServlet extends HttpServlet {
    
    @Inject
    private Service service;
    
    @Inject
    private Config config;

    @Override
    public void init() throws ServletException {
        Injector injector =
                GuiceFactory.getInjector();
        
        injector.injectMembers(this);
    }
    

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request,
                                  HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();

        Factory factory = Abdera.getNewFactory();
        Feed feed = factory.newFeed();
        feed.setLanguage("en-US");
        feed.setBaseUri("http://tasubo.com/");

        feed.setTitle("Tadas Šubonis dev blog");
        feed.addLink("http://tasubo.com/");
        feed.setUpdated(new Date());
        feed.addAuthor("Tadas Šubonis");
        feed.setId("urn:uuid:tadas-subonis-dev-blog");
        feed.addCategory("dev");
        
        List<Post> posts = service.getPosts();

        for (Post post : posts) {
            Entry entry = feed.insertEntry();
            entry.setTitle(post.getTitle());
            entry.addLink("/blog/view/" + post.getId() + "/" + post.getUrlString());
            entry.setId("urn:uuid:blogpost:" + post.getId());
            entry.setUpdated(post.getDate());
            entry.setSummary(post.getPreview());
        }


        feed.getDocument().writeTo(out);

        out.close();

    }

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
